package com.zjcode.cdcer.controller;

import com.chinatechstar.component.commons.controller.BaseController;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.supports.PageData;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.service.TaskAssignmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZQM
 * @date 2023/9/19 11:16
 */
@RestController
@Slf4j
@RequestMapping("/v1/task")
public class TaskAssignmentController extends BaseController {

    @Autowired
    private TaskAssignmentService taskAssignmentService;

    /**
     * 添加任务
     */
    @PostMapping("/add")
    public JsonObj add() {
        return taskAssignmentService.add(getPageData());
    }

    /**
     * 删除任务
     */
    @GetMapping("/del")
    public JsonObj del() {
        PageData pageData = getPageData();
        String id = (String) pageData.get("id");
        return taskAssignmentService.del(id);
    }

    /**
     * 修改任务
     */
    @PostMapping("/edit")
    public JsonObj edit() {
        return taskAssignmentService.edit(getPageData());
    }

    /**
     * 查询任务（单个）
     */
    @GetMapping("/selById")
    public JsonObj selById() {
        PageData pageData = getPageData();
        String id = (String) pageData.get("id");
        return taskAssignmentService.selById(id);
    }

    /**
     * 查询任务（列表）
     */
    @PostMapping("/selList")
    public JsonObj selList() {
        return taskAssignmentService.selList(getPageData());
    }

    /**
     * 封库
     */
    @GetMapping("/changeStatus")
    public JsonObj changeStatus() {
        PageData pageData = getPageData();
        String id = (String) pageData.get("id");
        String status = (String) pageData.get("status");
        return taskAssignmentService.changeStatus(id,status);
    }

    /**
     * 导出
     */
    @PostMapping("/queryAllExportData")
    public void queryAllExportData(HttpServletResponse response) {
        PageData pageData = getPageData();
        List<LinkedHashMap<String, Object>> dataList =
                (List<LinkedHashMap<String, Object>>) taskAssignmentService.queryAllExportDataBy(pageData)
                .getData()
                .get("data");
        // 组装表头
        List<String> headList = Arrays.asList(
                "省份",
                "城市",
                "区县",
                "机构名称",
                "开始时间",
                "结束时间",
                "筛查年度",
                "任务量");

        try {
            // 导出
            ExcelUtils.exportExcel(headList, dataList, "任务分配数据导出", response);
        } catch (Exception e) {
        }
    }
}
